<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="../vue_2.5.22.js"></script>
  <script src="../vue-router_3.0.2.js"></script>
</head>

<body>
  <div id="app">
    <router-link to="/user/1">user1</router-link>
    <router-link to="/user/2">user2</router-link>
    <router-link to="/user/3">user3</router-link>
    <router-link to="/register">register</router-link>

    <!-- 路由占位符 -->
    <router-view></router-view>

  </div>

  <script>
    // 路由组件
    const User = {
      template: '<h1>User 组件 -- 用户id为: {{$route.params.id}}</h1>'
    }

    const Register = {
      template: `
      <div>
        <h1>Register</h1>
        <hr/>
        <!-- 子路由链接 -->
        <router-link to='/register/tab1'>tab1</router-link>
        <router-link to='/register/tab2'>tab2</router-link>

        <!-- 子路由的占位符 -->
        <router-view></router-view>
      </div>
      `
    }

    const Tab1 = {
      template: '<h3>tab1组件</h3>'
    }

    const Tab2 = {
      template: '<h3>tab2组件</h3>'
    }

    // 创建路由实例对象
    const router = new VueRouter({
      // 所有的路由规则
      routes: [
        { path: '/', redirect: '/user' },
        { path: '/user/:id', component: User },
        // children 数组表示子路由规则
        {
          path: '/register', component: Register, children: [
            { path: '/register/tab1', component: Tab1 },
            { path: '/register/tab2', component: Tab2 }
          ]
        }
      ]
    })
    var vm = new Vue({
      el: '#app',
      data: {},
      router: router
      // router
      // 简写
      // 挂载路由实例对象
    })
  </script>
</body>

</html>